package com.embracesource.monitor.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.embracesource.monitor.dto.ServerDTO;
import com.embracesource.monitor.entity.Server;
import com.embracesource.monitor.service.IServerService;
import com.gnerv.pabu.basic.core.entity.Result;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 基础平台-服务器表 前端控制器
 * </p>
 *
 * @author Gnerv LiGen
 * @since 2019-09-03
 */
@RestController
@RequestMapping("/api/monitor/server")
public class ServerController {

    @Resource
    private IServerService serverService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@RequestBody Server server) {
        serverService.saveServer(server);
        return Result.response();
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Result create(@RequestBody Server server) {
        serverService.createServer(server);
        return Result.response();
    }

    @RequestMapping(value = "/delete/{serverId}", method = RequestMethod.GET)
    public Result delete(@PathVariable(value = "serverId") String serverId) {
        serverService.deleteServer(serverId);
        return Result.response();
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody Server server) {
        serverService.updateServer(server);
        return Result.response();
    }

    @RequestMapping(value = "/select/{serverId}", method = RequestMethod.GET)
    public Result select(@PathVariable(value = "serverId") String serverId) {
        ServerDTO server = serverService.selectServer(serverId);
        return Result.response().data("server", server);
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(Server server,
                       @RequestParam(value = "current", defaultValue = "1") Long current,
                       @RequestParam(value = "size", defaultValue = "300") Long size) {
        Page<ServerDTO> servers = serverService.listServer(server, current, size);
        return Result.response().data("servers", servers);
    }

}
